package tn.bergaoui.openwebapp.wicket.pages;

import java.sql.Blob;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;

import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialException;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.image.resource.BlobImageResource;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import tn.bergaoui.openwebapp.dao.PlayerDao;
import tn.bergaoui.openwebapp.dao.QuestionDao;
import tn.bergaoui.openwebapp.model.Player;
import tn.bergaoui.openwebapp.model.Question;
import tn.bergaoui.openwebapp.model.Response;
import tn.bergaoui.openwebapp.wicket.components.ISubmitListener;

public class PersonEditorPanel extends BaseBeanEditorPanel<Player, PlayerDao> {

	@SpringBean(name = "playerDao")
	private PlayerDao playerDao;

	@SpringBean(name = "questionDao")
	private QuestionDao questionDao;

	public PersonEditorPanel(String id, Long idplayer,
			ISubmitListener listener, Component parent) {
		super(id, idplayer, listener, parent);

		playerDao.getEntityManager().getTransaction().begin();
		List<Question> questions = questionDao.select();

		for (Question q : questions) {
			Response response = new Response();
			response.setPlayer(bean);
			response.setQuestion(q);
			if (bean.getResponses()==null)
				bean.setResponses(new HashSet<Response>());
			bean.getResponses().add(response);
		}
		bean = playerDao.save(bean);
		playerDao.getEntityManager().getTransaction().commit();

	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void addFormComponents(Form form) {

		form.add(new TextField("firstName", new PropertyModel(this.bean,
				"firstName")));
		form.add(new TextField("lastName", new PropertyModel(this.bean,
				"lastName")));

		BlobImageResource resource = new BlobImageResource() {

			/**
			 * 
			 */

			private static final long serialVersionUID = 1L;

			@Override
			protected Blob getBlob() {

				Blob blob = null;
				try {
					blob = new SerialBlob(bean.getPicture().getBytes());
				} catch (SerialException e) {

					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return blob;
			}
		};

		Image image = new Image("image", resource);

		IModel responsesModel = new LoadableDetachableModel() {
			@Override
			protected Object load() {
				return bean.getResponses();
			}

		};

		ListView responseList = new ListView("responses", responsesModel) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem item) {
				Response response = (Response) item.getModelObject();
				add(new Label("question", response.getQuestion().getContent()));
			}

		};

		form.add(responseList);
		form.add(image);
		
		playerDao.getEntityManager().getTransaction().begin();
		bean = playerDao.selectById(bean.getId());
		System.out.println("$$$$$$$$$$$$$$$$$$ " + bean.getResponses());
		playerDao.getEntityManager().getTransaction().commit();
	}

	@Override
	public PlayerDao getGenericDao() {
		return playerDao;
	}

	public void setPlayerDao(PlayerDao playerDao) {
		this.playerDao = playerDao;
	}

	public PlayerDao getPlayerDao() {
		return playerDao;
	}

	public void setQuestionDao(QuestionDao questionDao) {
		this.questionDao = questionDao;
	}

	public QuestionDao getQuestionDao() {
		return questionDao;
	}

}
